package 链表;

import Utils.ListNode;

/**
 * @author: TTDB
 * @create: 2025/9/16
 */
public class hasCycle {
    public boolean hasCycle(ListNode head) {
        // 双指针--快慢指针，这里是判断链表是否有环，快指针每次走两步，慢指针每次走一步，如果要入口那么就相遇后slow回头节点继续走即可，相遇既是入口
        if (head == null || head.next == null) {
            return false;
        }
        ListNode slow = head;
        ListNode fast = head.next;
        while (fast != null && fast.next != null) {
            if (slow == fast) {
                return true;
            }
            slow = slow.next;
            fast = fast.next.next;
        }
        return false;
    }
}
